//
//  EMModifyInfoViewController.m
//  ElectronicMall
//
//  Created by 史伟文 on 15/2/1.
//  Copyright (c) 2015年 cczu. All rights reserved.
//

#import "EMModifyInfoViewController.h"
#import "UINavigationController+EMNavi.h"
#import "AppDelegate.h"
#import "EMDotSegControl.h"
#import "EMInputTextField.h"
#import "EMSwitch.h"
#import "EMActionSheet.h"
#import "EMGetMemberInfoRespBody.h"
#import "MultipleChooseControl.h"
#import "EMSetMemberInfoReqBody.h"
#import "EMSetMemberInfoRespBody.h"
#import "MBProgressHUD.h"
#import "UserInfo.h"
@interface EMModifyInfoViewController ()
{
    AppDelegate *app;
    UIButton *chooseBirthBtn;
    EMInputTextField *qqNumberTF;
    EMInputTextField *emailTF;
    EMActionSheet *actionSheet;
    UIDatePicker *datePicker;
    MultipleChooseControl *multipleChoose;
    NSString *chooseGender;
    NSArray *loveArray;
}
@end

@implementation EMModifyInfoViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationMode:EMNavigationModeDefault andSubMode:0];
    self.view.backgroundColor = RGB(242, 242, 242);
    app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    self.navigationItem.title = @"个人信息修改";
    
    UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, 30, 32);
    [button setImage:[UIImage imageNamed:@"button_导航_返回_白"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithCustomView:button];
}
- (void)backAction: (UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    chooseGender = self.memberInfo.SEX;
    loveArray = @[@"苹果", @"桃子", @"梨", @"葡萄", @"车厘子", @"樱桃", @"凤梨", @"哈密瓜", @"菠萝", @"香蕉"];
    int height = 25 + 64;
    UILabel *genderTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, height, 80, 20)];
    genderTitleLabel.text = @"性别:";
    genderTitleLabel.textColor = [UIColor grayColor];
    genderTitleLabel.textAlignment = NSTextAlignmentLeft;
    genderTitleLabel.font = [UIFont fontWithName:HEITIMEDIUM size:16];
    [self.view addSubview:genderTitleLabel];
    EMDotSegControl *seg = [[EMDotSegControl alloc] initWithFrame:CGRectMake(70, height- 5, 80, 30)];
    if ([chooseGender isEqualToString:@"M"]) {
        seg.defaultIndex = 0;
    } else {
        seg.defaultIndex = 1;
    }
    [seg setTitleArray:@[@"男", @"女"]];
    [seg setActionBlock:^(NSUInteger gender) {
        if (gender == 0) {
            chooseGender = @"M";
        } else {
            chooseGender = @"F";
        }
    }];
    [self.view addSubview:seg];
    
    height += 40;
    UILabel *birthdayTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, height, 50, 20)];
    birthdayTitleLabel.text = @"生日:";
    birthdayTitleLabel.textColor = [UIColor grayColor];
    birthdayTitleLabel.textAlignment = NSTextAlignmentLeft;
    birthdayTitleLabel.font = [UIFont fontWithName:HEITIMEDIUM size:16];
    [self.view addSubview:birthdayTitleLabel];
    chooseBirthBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    chooseBirthBtn.frame = CGRectMake(70, height - 5, W(230), 30);
    chooseBirthBtn.layer.borderColor = RGB(112, 179, 81).CGColor;
    chooseBirthBtn.layer.borderWidth = 1;
    chooseBirthBtn.layer.cornerRadius = 5;
    [chooseBirthBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [chooseBirthBtn setTitle:self.memberInfo.BIRRHDAY forState:UIControlStateNormal];
    chooseBirthBtn.titleLabel.font = [UIFont fontWithName:HEITIMEDIUM size:14];
    chooseBirthBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    chooseBirthBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10);
    UIButton *but = [UIButton buttonWithType:UIButtonTypeSystem];
    but.frame = CGRectMake(chooseBirthBtn.frame.size.width - W(35), 0, W(35), 30);
    UIImage *arrow = [UIImage imageNamed:@"icon_菜单_展开"];
    [but setImage:[arrow imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    but.layer.borderColor = RGB(112, 179, 81).CGColor;
    but.layer.borderWidth = 1;
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:but.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = but.bounds;
    maskLayer.path = maskPath.CGPath;
    but.layer.mask = maskLayer;
    [chooseBirthBtn addSubview:but];
    [chooseBirthBtn addTarget:self action:@selector(chooseBirth:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:chooseBirthBtn];
    
    height += 40;
    UILabel *qqNumberTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, height, 50, 20)];
    qqNumberTitleLabel.text = @"QQ:";
    qqNumberTitleLabel.textColor = [UIColor grayColor];
    qqNumberTitleLabel.textAlignment = NSTextAlignmentLeft;
    qqNumberTitleLabel.font = [UIFont fontWithName:HEITIMEDIUM size:16];
    [self.view addSubview:qqNumberTitleLabel];
    qqNumberTF = [[EMInputTextField alloc] initWithFrame:CGRectMake(70, height - 5, W(230), 30)];
    qqNumberTF.placeholderText = @"请输入您的QQ号";
    [qqNumberTF setTextFieldMode:EMInputTextFieldModeText];
    qqNumberTF.inputTextField.delegate = self;
    qqNumberTF.inputTextField.text = self.memberInfo.QQ;
    [self.view addSubview:qqNumberTF];
    
    height += 40;
    UILabel *emailTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, height, 50, 20)];
    emailTitleLabel.text = @"邮箱:";
    emailTitleLabel.textColor = [UIColor grayColor];
    emailTitleLabel.textAlignment = NSTextAlignmentLeft;
    emailTitleLabel.font = [UIFont fontWithName:HEITIMEDIUM size:16];
    [self.view addSubview:emailTitleLabel];
    emailTF = [[EMInputTextField alloc] initWithFrame:CGRectMake(70, height - 5, W(230), 30)];
    emailTF.placeholderText = @"请输入您的邮箱";
    [emailTF setTextFieldMode:EMInputTextFieldModeText];
    emailTF.inputTextField.delegate = self;
    emailTF.inputTextField.text = self.memberInfo.EMAIL;
    [self.view addSubview:emailTF];
    
    height += 40;
    UILabel *favorTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, height, 50, 20)];
    favorTitleLabel.text = @"喜好:";
    favorTitleLabel.textColor = [UIColor grayColor];
    favorTitleLabel.textAlignment = NSTextAlignmentLeft;
    favorTitleLabel.font = [UIFont fontWithName:HEITIMEDIUM size:16];
    [self.view addSubview:favorTitleLabel];

    height += 30;
    multipleChoose = [[MultipleChooseControl alloc] initWithFrame:CGRectMake(40, height, 260, 200)];
    [multipleChoose setTitleArray:loveArray];
    [self.view addSubview:multipleChoose];
    
    height += 220;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(W(10), H(height), W(300), H(40));
    btn.backgroundColor = RGB(112, 179, 81);
    btn.layer.cornerRadius = 5;
    btn.titleLabel.font = [UIFont fontWithName:HEITIMEDIUM size:16];
    [btn setTitle:@"提交修改" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(handupCorrectAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    
    datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 100, 280, 100)];
    datePicker.datePickerMode = UIDatePickerModeDate;
    datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh"];
    
    actionSheet =[[EMActionSheet alloc]initWithFrame:CGRectMake(0,[UIScreen mainScreen].bounds.size.height , [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    [actionSheet initWithTitle:@"选择生日" andContentView:datePicker];
    
    UIButton *chooseCityBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    chooseCityBtn.frame = CGRectMake(W(255), [UIScreen mainScreen].bounds.size.height - H(250), W(50), H(30));
    chooseCityBtn.backgroundColor = RGB(112, 179, 81);
    chooseCityBtn.layer.cornerRadius = 5;
    chooseCityBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [chooseCityBtn setTitle:@"选好了" forState:UIControlStateNormal];
    [chooseCityBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [chooseCityBtn addTarget:self action:@selector(confirmChooseAction:) forControlEvents:UIControlEventTouchUpInside];
    [actionSheet addSubview:chooseCityBtn];

}

#pragma mark - 点击事件
- (void)chooseBirth:(UIButton *)sender
{
    [UIView animateWithDuration:0.3 animations:^{
        actionSheet.frame =CGRectMake(0,0 ,[UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        [self.navigationController.view addSubview:actionSheet];
    }];
}
- (void)confirmChooseAction:(UIButton *)sender
{
    [actionSheet removeActionSheet];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"MM-dd";
    [chooseBirthBtn setTitle:[formatter stringFromDate:datePicker.date] forState:UIControlStateNormal];
    
}

- (void)handupCorrectAction:(UIButton *)sender
{
    if (!chooseGender) {
        [app showTip:@"请选择性别"];
        return;
    }
    if (!chooseBirthBtn.titleLabel.text) {
        [app showTip:@"请选择生日"];
        return;
    }
    if (!qqNumberTF.inputTextField.text) {
        [app showTip:@"请输入QQ号"];
        return;
    }
    if (!emailTF.inputTextField.text) {
        [app showTip:@"请输入邮箱"];
        return;
    }
    [MBProgressHUD  showHUDAddedTo:self.navigationController.view animated:YES];
    EMSetMemberInfoReqBody *reqBody = [[EMSetMemberInfoReqBody alloc] init];
    reqBody.JUDGEMETHOD = @"UPDATE-USER-INFO";
    reqBody.USERID = [UserInfo sharedUserInfo].m_userName;
    reqBody.SESSION = [UserInfo sharedUserInfo].m_session;
    reqBody.SEX = chooseGender;
    reqBody.BIRTHDAY = chooseBirthBtn.titleLabel.text;
    reqBody.QQ = qqNumberTF.inputTextField.text;
    reqBody.EMAIL = emailTF.inputTextField.text;
    NSArray *chooseLoveArray = multipleChoose.chooseArray;
    NSMutableString *loveString = [[NSMutableString alloc] init];   
    for (int i = 0; i < chooseLoveArray.count; i ++) {
        int index = [chooseLoveArray[i] intValue];
        [loveString appendFormat:@"%@,", loveArray[index]];
    }
    reqBody.LOVE = loveString;
    
    HessianFormDataRequest *request = [[HessianFormDataRequest alloc] init];
    request.postData = reqBody.keyValues;
    [request startRequestWithURL:baseURL andCompletionBlcok:^(NSDictionary *respInfo) {
        EMSetMemberInfoRespBody *respBody = [EMSetMemberInfoRespBody objectWithKeyValues:respInfo];
        [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
        NSLog(@"%@", respInfo);
        if ([respBody.ERRORCODE isEqualToString: @"0000"]) {
            [app showTip:respBody.SUCCESSFULINFO];
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [app showTip:respInfo[@"ERRORDESTRIPTION"]];
        }
        
    } andFailedBlock:^{
        
        [app showTip:@"无法连接服务器、请稍后再试"];
    }];
    

}
@end
